package cn.lingyangwl.agile.tenant;

import cn.lingyangwl.agile.tenant.core.ITenantDataHandler;
import cn.lingyangwl.agile.tenant.core.TenantDataIsolateService;
import cn.lingyangwl.agile.tenant.core.TenantDataIsolateServiceImpl;
import cn.lingyangwl.framework.core.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shenguangyang
 */
@Slf4j
@Configuration
public class TenantMainConfig implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (SpringUtils.getBeansOfType(ITenantDataHandler.class).isEmpty()) {
            log.warn("================> 当前工程模块中尚未包含 system-api 依赖");
        }
    }

    @Bean
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public MybatisConfig mybatisConfig() {
        return new MybatisConfig();
    }

    @Bean
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public TenantDataIsolateService tenantDataIsolateService() {
        return new TenantDataIsolateServiceImpl();
    }

    /**
     * @author shenguangyang
     */
    @Slf4j
    @MapperScan(basePackages = "cn.lingyangwl.agile.tenant.core.mapper")
    public static class MybatisConfig {

    }
}
